/**  
* @Title: TimerConfig.java
* @Package com.lemon.timer
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年5月22日
* @version V1.0  
*/
package com.lemon.timer;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年5月22日
 * @版本 V 1.0 
 */
/*
 * @Configuration
 * @EnableScheduling
 */
public class TimerConfig  implements SchedulingConfigurer, AsyncConfigurer{
	
	/*
	* 并行任务
	*/
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar)
	{	
		System.out.println("定时任务-并行多线程开启...");
		TaskScheduler taskScheduler = taskScheduler();
		taskRegistrar.setTaskScheduler(taskScheduler);
	}

	/**
	* 并行任务使用策略：多线程处理（配置线程数等）
	* 
	* @return ThreadPoolTaskScheduler 线程池
	*/
	@Bean(destroyMethod = "shutdown")
	public ThreadPoolTaskScheduler taskScheduler()
	{
		System.out.println("配置TaskScheduler并行任务...");
		ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
		scheduler.setPoolSize(20);
		scheduler.setThreadNamePrefix("task-");  //设置线程名开头
		scheduler.setAwaitTerminationSeconds(60);
		scheduler.setWaitForTasksToCompleteOnShutdown(true);
		return scheduler;
	}

	/*
	* 异步任务
	*/
	public Executor getAsyncExecutor()
	{	
		System.out.println(3000000);
		Executor executor = taskScheduler();
		return executor;
	}

	/*
	* 异步任务 异常处理
	*/
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler()
	{
		return new SimpleAsyncUncaughtExceptionHandler();
	}
}
